import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { APIBase, ListOption } from 'app/modules/base/api-base';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class RestrictedRecordService implements Resolve<any> {

  constructor(
    private http: HttpClient
  ) { }

  async resolve(route: ActivatedRouteSnapshot) {
    const page = Number(route.queryParamMap.get('page')) || 1;
    const count = Number(route.queryParamMap.get('count')) || 10;
    const options: RestrictedRecordOption = { page, count };
    return this.records<RestrictedRecord>(route, options);
  }

  records<T>(route: ActivatedRouteSnapshot, options: RestrictedRecordOption) {
    this.parseUsbParams(route, options);
    let base = new APIBase<any, T>(this.http, '/api/admin/usb/record');
    return base.list(options);
  }

  parseUsbParams(route: ActivatedRouteSnapshot, options: RestrictedRecordOption) {
    let keys: string[] = ['area_id', 'keywords'];
    this.doParseParams(route, options, keys);
  }
  
  doParseParams(route: ActivatedRouteSnapshot, options: RestrictedRecordOption, keys: string[]) {
    for (let k of keys) {
      let v = route.queryParamMap.get(k);
      if (v) {
        (options as any)[k] = v;
      }
    }
  }
  
  // 日志列表
  logs<T>() {
    return new APIBase<any, T>(this.http, `/api/admin/usb/log`);
  }

}

export interface RestrictedRecord {
  id: number,
  name: string,
  pc_name: string,
  user_name: string,
  vendor_id: string,
  product_id: string,
  serial_number: string,
  inserted_at: string,
  in_whitelist: boolean,
  authorized_at: string,
  is_authorized: boolean,
  whitelist_id: number,
  pc_id: number
}

export interface RestrictedRecordOption extends ListOption {
  area_id?: number;
  keywords?: string;
}